// rs_anwser/pages/subject/subject.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageIndex: 1,
    current: 0,
    pageSize: 10,
    wrongCount: 0,
    rightCount: 0,
    total: 160,
    collectPrompt: true,
    
    subjectList: [
     
    ],
    param: {
      subjectType: 'mutilple'
    }

  },

  updateCurrent: function (data) {
    this.setData({ current: data.detail });
  },
  updateSubjectList: function (data) {
    this.setData({ subjectList: data.detail });

  },

  changeSubject: function (e) {
    // this.setData({ current: e.detail.current });
    // if (e.detail.current == this.data.subjectList.length - 1) {
    //   var me = this;
      this.setData({ current: e.detail.current, subjectIndex: e.detail.current + 1 });
      if (e.detail.current == this.data.subjectList.length - 1) {
        app.getData(this, "entry/wxapp/getSubjectList", { subjectType: 'mutilple' });
      }
    // }

  },

  /**
  * 
  */
  collect: function (e) {
    app.collect('mutilple', e.currentTarget.dataset.id);
  },

  /**
   * 选择题目选项
   */
  selectOption: function (e) {


    var subject = app.subjectPage.getCurrentSubject(this);
    if (typeof(subject.result) == "undefined") {
      //如果还没有没有答题
      var answer = subject.anwser;
      var selectOption = e.currentTarget.dataset.label;
      var index = e.currentTarget.dataset.index;
      var options = subject.options;
      var selectedOptions = subject.selectedOptions;
      selectedOptions = selectedOptions ? selectedOptions.split(','):[];
     
      if (selectOption == options[index].label && selectedOptions.indexOf(selectOption) == -1) {
          options[index]['resultClass'] = 'result-right';
          selectedOptions.push(options[index].label);
          
      }
    
      subject.options = options;
      subject.selectedOptions = selectedOptions.join();
      app.subjectPage.updateCurrentSubject(this,subject);
    }
  },

  /**
   * 确定选择的答案
   */
  confirm:function(e){     
      var subject = app.subjectPage.getCurrentSubject(this);
      if (!subject.result){
        var selectedOptions = subject.selectedOptions.split(',');
        var answer = subject.anwser.split(',');
        if (selectedOptions.sort().toString() == answer.sort().toString()) {
          subject.result = 1;
          this.setData({rightCount:++this.data.rightCount});
          subject.resultColor = 'color:green';
          wx.showToast({
            title: '答题正确',
          })
        } else {
          subject.result = 2;
          this.setData({ wrongCount: ++this.data.wrongCount });
          subject.resultColor = 'color:red';
          //选错则添加到错题本
          app.addWrong('mutilple', subject.id);
        }
        app.subjectPage.updateCurrentSubject(this,subject);
      }
     
      


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var me = this;
    app.getData(this, "entry/wxapp/getSubjectList", { subjectType: 'mutilple' });
    var date = new Date();
    var userInfo = wx.getStorageSync('userInfo');
    this.setData({
      refreshTime: date.toLocaleTimeString(),
      collectPrompt: userInfo.prompt.collect_prompt
    })



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


 
})